using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Toolbox.DnsService.Events;
using Toolbox.DnsService.Models;

namespace Toolbox.DnsService.Interfaces
{
    /// <summary>
    /// DNS服务接口
    /// 定义DNS服务的核心功能
    /// 作者：huobing
    /// </summary>
    public interface IDnsService : IDisposable
    {
        /// <summary>
        /// 获取服务是否正在运行
        /// </summary>
        bool IsRunning { get; }
        
        /// <summary>
        /// 获取监听端口
        /// </summary>
        int Port { get; }
        
        /// <summary>
        /// 获取DNS缓存记录
        /// </summary>
        Dictionary<string, string> DnsCache { get; }
        
        /// <summary>
        /// DNS请求处理事件
        /// </summary>
        event EventHandler<DnsRequestEventArgs>? DnsRequestProcessed;
        
        /// <summary>
        /// 服务状态变化事件
        /// </summary>
        event EventHandler<DnsServiceStatusEventArgs>? StatusChanged;
        
        /// <summary>
        /// 启动DNS服务
        /// </summary>
        /// <returns>是否启动成功</returns>
        bool Start();
        
        /// <summary>
        /// 停止DNS服务
        /// </summary>
        void Stop();
        
        /// <summary>
        /// 添加DNS记录
        /// </summary>
        /// <param name="domain">域名</param>
        /// <param name="ip">IP地址</param>
        /// <returns>是否添加成功</returns>
        bool AddDnsRecord(string domain, string ip);
        
        /// <summary>
        /// 移除DNS记录
        /// </summary>
        /// <param name="domain">域名</param>
        /// <returns>是否移除成功</returns>
        bool RemoveDnsRecord(string domain);
        
        /// <summary>
        /// 清除所有DNS记录
        /// </summary>
        void ClearDnsRecords();
        
        /// <summary>
        /// 测试DNS服务器连接
        /// </summary>
        /// <param name="dnsServer">DNS服务器地址</param>
        /// <param name="timeout">超时时间（毫秒）</param>
        /// <returns>测试结果</returns>
        Task<DnsTestResult> TestDnsServerAsync(string dnsServer, int timeout = 5000);
        
        /// <summary>
        /// 设置日志记录器
        /// </summary>
        /// <param name="logger">日志记录器</param>
        void SetLogger(IDnsLogger logger);
        
        /// <summary>
        /// 启用日志记录
        /// </summary>
        /// <param name="enable">是否启用</param>
        void EnableLogging(bool enable);
    }
    
    /// <summary>
    /// DNS日志记录器接口
    /// 作者：huobing
    /// </summary>
    public interface IDnsLogger : IDisposable
    {
        /// <summary>
        /// 记录状态信息
        /// </summary>
        /// <param name="message">消息内容</param>
        void LogStatus(string message);
        
        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="exception">异常对象</param>
        void LogError(string message, Exception? exception = null);
    }
    
    /// <summary>
    /// DNS配置管理器接口
    /// 作者：huobing
    /// </summary>
    public interface IDnsConfigManager : IDisposable
    {
        /// <summary>
        /// 加载配置
        /// </summary>
        /// <returns>DNS服务配置</returns>
        DnsServiceConfig LoadConfig();
        
        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="config">DNS服务配置</param>
        void SaveConfig(DnsServiceConfig config);
        
        /// <summary>
        /// 获取设置值
        /// </summary>
        /// <param name="key">设置键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>设置值</returns>
        string GetSetting(string key, string defaultValue = "");
        
        /// <summary>
        /// 保存设置值
        /// </summary>
        /// <param name="key">设置键</param>
        /// <param name="value">设置值</param>
        void SaveSetting(string key, string value);
        
        /// <summary>
        /// 获取本地DNS服务端口
        /// </summary>
        /// <returns>端口号</returns>
        int GetLocalDnsPort();
        
        /// <summary>
        /// 设置本地DNS服务端口
        /// </summary>
        /// <param name="port">端口号</param>
        /// <returns>是否设置成功</returns>
        bool SetLocalDnsPort(int port);
        
        /// <summary>
        /// 获取上游DNS服务器列表
        /// </summary>
        /// <returns>DNS服务器列表</returns>
        List<string> GetUpstreamDnsServers();
        
        /// <summary>
        /// 添加上游DNS服务器
        /// </summary>
        /// <param name="dnsServer">DNS服务器地址</param>
        /// <returns>是否添加成功</returns>
        bool AddUpstreamDnsServer(string dnsServer);
        
        /// <summary>
        /// 移除上游DNS服务器
        /// </summary>
        /// <param name="dnsServer">DNS服务器地址</param>
        /// <returns>是否移除成功</returns>
        bool RemoveUpstreamDnsServer(string dnsServer);
        
        /// <summary>
        /// 获取DNS记录字典
        /// </summary>
        /// <returns>DNS记录字典</returns>
        Dictionary<string, string> GetDnsRecords();
        
        /// <summary>
        /// 添加DNS记录
        /// </summary>
        /// <param name="domain">域名</param>
        /// <param name="ip">IP地址</param>
        /// <returns>是否添加成功</returns>
        bool AddDnsRecord(string domain, string ip);
        
        /// <summary>
        /// 移除DNS记录
        /// </summary>
        /// <param name="domain">域名</param>
        /// <returns>是否移除成功</returns>
        bool RemoveDnsRecord(string domain);
        
        /// <summary>
        /// 获取是否启用DNS缓存
        /// </summary>
        /// <returns>是否启用缓存</returns>
        bool GetEnableCache();
        
        /// <summary>
        /// 设置是否启用DNS缓存
        /// </summary>
        /// <param name="enable">是否启用</param>
        /// <returns>是否设置成功</returns>
        bool SetEnableCache(bool enable);
        
        /// <summary>
        /// 获取缓存过期时间（分钟）
        /// </summary>
        /// <returns>缓存过期时间</returns>
        int GetCacheExpirationMinutes();
        
        /// <summary>
        /// 设置缓存过期时间（分钟）
        /// </summary>
        /// <param name="minutes">过期时间</param>
        /// <returns>是否设置成功</returns>
        bool SetCacheExpirationMinutes(int minutes);
        
        /// <summary>
        /// 获取是否启用自动启动
        /// </summary>
        /// <returns>是否启用自动启动</returns>
        bool GetAutoStart();
        
        /// <summary>
        /// 设置是否启用自动启动
        /// </summary>
        /// <param name="autoStart">是否启用</param>
        /// <returns>是否设置成功</returns>
        bool SetAutoStart(bool autoStart);
        
        /// <summary>
        /// 获取是否启用日志记录
        /// </summary>
        /// <returns>是否启用日志记录</returns>
        bool GetEnableLogging();
        
        /// <summary>
        /// 设置是否启用日志记录
        /// </summary>
        /// <param name="enable">是否启用</param>
        /// <returns>是否设置成功</returns>
        bool SetEnableLogging(bool enable);
        
        /// <summary>
        /// 重置为默认配置
        /// </summary>
        /// <returns>是否重置成功</returns>
        bool ResetToDefaults();
    }
}